package com.yayohos.locationmanager.providers.permissionprovider;


import com.yayohos.locationmanager.constants.RequestCode;
import com.yayohos.locationmanager.helper.LogUtils;
import com.yayohos.locationmanager.listener.DialogListener;
import com.yayohos.locationmanager.providers.dialogprovider.DialogProvider;

import ohos.bundle.IBundleManager;

/**
 * DefaultPermissionProvider
 *
 * @date 2021/5/26
 */
public class DefaultPermissionProvider extends PermissionProvider implements DialogListener {
    private PermissionCompatSource permissionCompatSource;

    public DefaultPermissionProvider(String[] requiredPermissions, DialogProvider dialogProvider) {
        super(requiredPermissions, dialogProvider);
    }

    @Override
    public boolean requestPermissions() {
        if (getAbility() == null) {
            LogUtils.logI("Cannot ask for permissions, "
                    + "because DefaultPermissionProvider doesn't contain an Ability instance.");
            return false;
        }
        if (shouldShowRequestPermissionRationale()) {
            getDialogProvider().setDialogListener(this);
            getDialogProvider().getDialog(getAbility()).show();
        } else {
            executePermissionsRequest();
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == RequestCode.RUNTIME_PERMISSION) {
            boolean isDenied = false;
            for (int i = 0, size = permissions.length; i < size; i++) {
                if (grantResults[i] != IBundleManager.PERMISSION_GRANTED) {
                    isDenied = true;
                    break;
                }
            }
            if (isDenied) {
                LogUtils.logI("User denied some of required permissions, task will be aborted!");
                if (getPermissionListener() != null) {
                    getPermissionListener().onPermissionsDenied();
                }
            } else {
                LogUtils.logI("We got all required permission!");
                if (getPermissionListener() != null) {
                    getPermissionListener().onPermissionsGranted();
                }
            }
        }
    }

    @Override
    public void onPositiveButtonClick() {
        LogUtils.logI("User  let us to ask for permission!");
        executePermissionsRequest();
    }

    @Override
    public void onNegativeButtonClick() {
        LogUtils.logI("User didn't even let us to ask for permission!");
        if (getPermissionListener() != null) {
            getPermissionListener().onPermissionsDenied();
        }
    }

    boolean shouldShowRequestPermissionRationale() {
        boolean shouldShowRationale = false;
        for (String permission : getRequiredPermissions()) {
            shouldShowRationale = shouldShowRationale || checkRationaleForPermission(permission);
        }
        return shouldShowRationale && getAbility() != null && getDialogProvider() != null;
    }

    /**
     * checkRationaleForPermission
     *
     * @param permission 权限
     * @return boolean should Show Request Permission Rationale
     */
    boolean checkRationaleForPermission(String permission) {
        if (getAbility() != null) {
            return getPermissionCompatSource().shouldShowRequestPermissionRationale(getAbility(), permission);
        } else {
            return false;
        }
    }

    /**
     * 申请权限
     */
    void executePermissionsRequest() {
        LogUtils.logI("Asking for Runtime Permissions...");
        if (getAbility() != null) {
            getPermissionCompatSource().requestPermissions(getAbility(),
                    getRequiredPermissions(), RequestCode.RUNTIME_PERMISSION);
        } else {
            LogUtils.logE("Something went wrong requesting for permissions.");
            if (getPermissionListener() != null){
                getPermissionListener().onPermissionsDenied();
            }
        }
    }

    /**
     * 获取PermissionCompatSource
     *
     * @return PermissionCompatSource permissionCompatSource
     */
    protected PermissionCompatSource getPermissionCompatSource() {
        if (permissionCompatSource == null) {
            permissionCompatSource = new PermissionCompatSource();
        }
        return permissionCompatSource;
    }

}
